@file:OptIn(ExperimentalNativeApi::class, ExperimentalForeignApi::class)
package com.example.kotlin_library

import androidx.compose.foundation.layout.*
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.ComposeArkUIViewController
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.coroutines.initMainHandler
import platform.ohos.napi_env
import platform.ohos.napi_value
import kotlin.experimental.ExperimentalNativeApi

/**
 * UniversalAsyncImage 在 OHOS 上的测试示例
 *
 * 测试不同格式的图片加载：
 * 1. HEIF 格式
 * 2. WebP 格式
 * 3. JPEG 格式
 */
@Composable
 fun UniversalAsyncImageTestSampleOhos() {
    println("[UniversalAsyncImageTest] Composable start.")
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        Text(
            text = "UniversalAsyncImage OHOS 测试",
            fontSize = 20.sp,
            color = Color.Black
        )

        // 示例1: HEIF 格式图片
        Text("示例1: HEIF 格式", fontSize = 14.sp)
        UniversalAsyncImageOhosWithState(
            url = "https://sns-na-i6.xhscdn.com/1040g2sg319iqevppne705ne4r6og8outofq2gj0?imageView2/2/w/1440/format/heif/q/45&redImage/frame/0&ap=5&sc=SRH_DTL&sign=9baae1f30aa9513a12dbfc4d5344df0a&t=68d3b876",
            description = "HEIF 图片"
        )

        // 示例2: WebP 格式图片
        Text("示例2: WebP 格式", fontSize = 14.sp)
        UniversalAsyncImageOhosWithState(
            url = "https://i-blog.csdnimg.cn/direct/0053b18545c84151a8ba37243803cdff.webp",
            description = "WebP 图片"
        )

//        // 示例3: 小尺寸图片
//        Text("示例3: 多张小图", fontSize = 14.sp)
//        Row(
//            horizontalArrangement = Arrangement.spacedBy(8.dp)
//        ) {
//            repeat(3) { index ->
//                UniversalAsyncImage(
//                    url = "https://picsum.photos/100/100?random=$index",
//                    contentDescription = "小图$index",
//                    modifier = Modifier.size(100.dp)
//                )
//            }
//        }
    }
    println("[UniversalAsyncImageTest] Composable end.")
}

@Composable
fun UniversalAsyncImageShowcase() {
    UniversalAsyncImageTestSampleOhos()
}

/**
 * 带加载状态的 OHOS UniversalAsyncImage 示例
 */
@Composable
fun UniversalAsyncImageOhosWithState(url: String, description: String) {
    println("[UniversalAsyncImageTest] prepare item url=$url description=$description")
    var loadState by remember { mutableStateOf("等待加载") }

    Column(
        modifier = Modifier.fillMaxWidth(),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        UniversalAsyncImage(
            url = url,
            contentDescription = description,
            modifier = Modifier
                .fillMaxWidth()
                .height(200.dp),
            onLoading = {
                loadState = "加载中..."
                println("OHOS Test: 开始加载 - $url")
            },
            onSuccess = {
                loadState = "加载成功"
                println("OHOS Test: 加载成功 - $url")
            },
            onError = { error ->
                loadState = "加载失败: ${error?.message}"
                println("OHOS Test: 加载失败 - $url, Error: ${error?.message}")
            }
        )

        Spacer(modifier = Modifier.height(8.dp))

        Text(
            text = "状态: $loadState",
            fontSize = 12.sp,
            color = when {
                loadState.contains("成功") -> Color.Green
                loadState.contains("失败") -> Color.Red
                else -> Color.Gray
            }
        )
    }
}

